<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <!--表格开始-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" width="150px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="100px" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.header" style="width: 30px;height: 30px;">
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status === 0" type="danger">冻结</el-tag>
          <el-tag v-if="scope.row.status === 2" type="danger">禁言</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="时间" width="250px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ renderTime(scope.row.createdAt) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户名" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.phone }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="经验" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="danger">Exp:{{ scope.row.experience }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="等级" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.level }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="320px" align="center">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="add(scope.row)">增加经验</el-button>
          <el-button v-if="scope.row.status === 1" type="danger" size="mini" @click="freeze(scope.row.id,'冻结',1)">冻结</el-button>
          <el-button v-if="scope.row.status === 0" type="success" size="mini" @click="freeze(scope.row.id,'解冻',2)">解冻</el-button>
          <el-button v-if="scope.row.status !== 0 && scope.row.status !== 2" type="danger" size="mini" @click="freeze(scope.row.id,'禁言',3)">禁言</el-button>
          <el-button v-if="scope.row.status === 2" type="success" size="mini" @click="freeze(scope.row.id,'解冻',2)">解禁</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--表格结束-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getUser" />

    <!-- 弹框 -->
    <el-dialog
      v-loading="listLoading"
      title="增加经验"
      :visible.sync="alert.centerDialogVisible"
      :before-close="close"
      width="30%"
    >
      <div class="input">
        <el-input v-model="alert.uname" placeholder="请输入" disabled>
          <template slot="prepend">用户名：</template>
        </el-input>
      </div>
      <div class="input">
        <el-input v-model="alert.experience" placeholder="请输入" type="number" @keyup.enter.native="sureDialogVisible">
          <template slot="prepend">经验：</template>
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureDialogVisible">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getUser, freeze, deletes, add } from '@/api/user'
import { renderTime } from '../../utils'
export default {
  name: 'Index',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      collection: '',
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        keywords: undefined
      },
      alert: {
        uname: '',
        experience: '',
        id: '',
        centerDialogVisible: false
      }

    }
  },
  created() {
    this.getUser()
  },
  methods: {
    renderTime,
    // 获取数据
    getUser() {
      this.listLoading = true
      getUser(this.listQuery).then(res => {
        const { data: { count, rows }} = res.data
        this.total = count
        this.list = rows
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页
    handleFilter() {
      this.listQuery.page = 1
      this.getUser()
    },
    // 冻结
    freeze(id, msg, status) {
      this.$confirm('此操作将' + msg + '该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        freeze(Number(id), { status: Number(status) })
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.getUser()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 删除
    del(id) {
      this.$confirm('此操作将删除该用户,并会将其与之相关的数据全部删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletes(id)
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.getUser()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    add(resp) {
      this.alert.centerDialogVisible = true
      this.alert.id = resp.id
      this.alert.experience = resp.experience
      this.alert.uname = resp.name
    },
    close() {
      this.alert.centerDialogVisible = false
      this.listLoading = false
    },
    sureDialogVisible() {
      this.listLoading = true
      add(+this.alert.id, {
        experience: +this.alert.experience,
        id: +this.alert.id
      }).then(res => {
        const { msg } = res.data
        this.$message.success(msg)
        this.close()
        this.getUser()
      }).catch(() => {
        this.close()
      })
    }
  }
}
</script>
<style >
.app-container .input {
    margin-top: 20px;
  }
</style>
